
var sysurl = 'http://192.168.0.51:8091/api/';

var sysurl2 = 'http://192.168.0.51:8091/m/';
var url = 'http://192.168.0.51:8091/';


// function ajax(obj){
// 	obj = obj || {};
// 	$.ajax({
// 		url:url+obj.url,
// 		type:obj.type||'post',
// 		headers:{
// 			xhrFields: {
// 				withCredentials: true
// 			},
// 			crossDomain: true,
// 		},
// 		dataType:'json',
// 		data:obj.data,
// 		success:obj.success,
// 		error:obj.error
// 	})
// }

/***
 * banner
 * @param type 类型1首页2商城3歌曲4网站5套曲6舞曲
 * @param callBack
 */
// function banner(type,callBack) {
// 	ajax({
// 		url: '/api/banner/list',
// 		data: {
// 			type: type
// 		}, success: callBack
// 	})
// }

/**
 * 设置cookie
 * @param {type} name
 * @param {type} value
 * @returns {undefined}
 */
function setCookie(name, value) {
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString();
}

/**
 * 获取cookie
 * @param {type} name
 * @returns {unresolved}
 */
function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if (arr = document.cookie.match(reg)) {
		return unescape(arr[2]);
	}
	return null;
}

/**
 * 删除cookie
 * @param {type} name
 * @returns {undefined}
 */
function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if (cval !== null)
		document.cookie = name + "=" + cval + ";path=/;expires=" + exp.toGMTString();
}

/**
 * 获取链接参数
 * @param {type} name
 * @returns {unresolved}
 */
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r !== null)
		return  unescape(r[2]);
	return null;
}
if(getCookie("token") !== null){
	axios.defaults.headers.token = getCookie("token")
}

axios.defaults.retry = 4;
axios.defaults.retryDelay = 1000;
axios.defaults.headers.common['X-Requested-With'] = "XMLHttpRequest";

axios.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';


var index;
//添加请求拦截器
axios.interceptors.request.use(config => {
	//在发送请求之前做某事，比如说 设置loading动画显示
	// index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
	return config
}, error => {
	//请求错误时做些事
	return Promise.reject(error)
})

axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
	var config = err.config;
	// If config does not exist or the retry option is not set, reject
	if(!config || !config.retry) return Promise.reject(err);

	// Set the variable for keeping track of the retry count
	config.__retryCount = config.__retryCount || 0;

	// Check if we've maxed out the total number of retries
	if(config.__retryCount >= config.retry) {
		// Reject with the error
		return Promise.reject(err);
	}

	// Increase the retry count
	config.__retryCount += 1;

	// Create new promise to handle exponential backoff
	var backoff = new Promise(function(resolve) {
		setTimeout(function() {
			resolve();
		}, config.retryDelay || 1);
	});

	// Return the promise in which recalls axios to retry the request
	return backoff.then(function() {
		return axios(config);
	});
});

//添加响应拦截器
axios.interceptors.response.use(response => {
	//对响应数据做些事，比如说把loading动画关掉
	// layer.close(index);
	if(response.data.code == 205){
		setTimeout(function () {
			delCookie("token");
			// window.location.href='login.html';
			overy_show()
			$(".login-mask").fadeIn()
		}, 3000);



	}
	return response
}, error => {
	//请求错误时做些事
	// layer.close(index);
	return Promise.reject(error)
})
